package com.hd123.rumba.gwt.widget.client.grid;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class DxGridColumnDef {
  private DxGrid owner;
  private String caption;
  private String headStyleName = "DxGrid-ColumnHeader";
  private Widget headerWidget = null;
  private boolean enterStop = true;
  private boolean readOnly = true;
  private CellRendererFactory rendererFactory = null;
  private CellEditor editor = null;
  private String width = "";
  private int summaryMethod;
  private boolean visible = true;

  public DxGridColumnDef() {
  }

  public DxGridColumnDef(String caption) {
    this.caption = caption;
  }

  public DxGrid getOwner() {
    return owner;
  }

  public void setOwner(DxGrid owner) {
    this.owner = owner;
  }

  public String getHeadStyleName() {
    return headStyleName;
  }

  public void setHeadStyleName(String headStyleName) {
    this.headStyleName = headStyleName;
  }

  public Widget getHeaderWidget() {
    return headerWidget;
  }

  public void setHeaderWidget(Widget headerWidget) {
    this.headerWidget = headerWidget;
  }

  public void setEnterStop(boolean b) {
    this.enterStop = b;
  }

  public void setReadOnly(boolean b) {
    this.readOnly = b;
    if (readOnly) {
      if (rendererFactory != null) {
        if ("DxGrid-Cell".equals(rendererFactory.getStyleName()))
          rendererFactory.setStyleName("DxGrid-Cell-ReadOnly");
        if ("DxGrid-Cell-Selected".equals(rendererFactory.getStyleName()))
          rendererFactory.setStyleName("DxGrid-Cell-ReadOnly-Selected");
      }
      if (editor != null) {
        if ("DxGrid-CellEditor".equals(editor.getStyleName()))
          editor.setStyleName("DxGrid-CellEditor-ReadOnly");
      }
    } else {
      if (rendererFactory != null) {
        if ("DxGrid-Cell-ReadOnly".equals(rendererFactory.getStyleName()))
          rendererFactory.setStyleName("DxGrid-Cell");
        if ("DxGrid-Cell-ReadOnly-Selected".equals(rendererFactory.getStyleName()))
          rendererFactory.setStyleName("DxGrid-Cell-Selected");
      }
      if (editor != null) {
        if ("DxGrid-CellEditor-ReadOnly".equals(editor.getStyleName()))
          editor.setStyleName("DxGrid-CellEditor");
      }
    }
  }

  public void setEditor(CellEditor editor) {
    this.editor = editor;
  }

  public void setRendererFactory(CellRendererFactory rendererFactory) {
    this.rendererFactory = rendererFactory;
  }

  public String getCaption() {
    return caption;
  }

  public void setCaption(String caption) {
    this.caption = caption;
  }

  public CellEditor getEditor() {
    return editor;
  }

  public boolean isEnterStop() {
    return enterStop;
  }

  public boolean isReadOnly() {
    return readOnly;
  }

  public CellRendererFactory getRendererFactory() {
    return rendererFactory;
  }

  public String getWidth() {
    return width;
  }

  public void setWidth(String width) {
    this.width = width;
  }

  public int getSummaryMethod() {
    return summaryMethod;
  }

  public void setSummaryMethod(int summaryMethod) {
    this.summaryMethod = summaryMethod;
  }
  
  public boolean isVisible() {
    return visible;
  }

  public void setVisible(boolean visible) {
    this.visible = visible;
  }

  public static class Selection extends DxGridColumnDef implements ClickListener {
    private CheckBoxRenderer widget = new CheckBoxRenderer();

    public Selection(DxGrid owner) {
      setOwner(owner);
      setRendererFactory(new CheckBoxRendererFactory());
      setEditor(new CheckBoxEditor());
      setEnterStop(false);
      setWidth("30px");
      setHeaderWidget(widget);
      widget.setChecked(false);
      widget.addClickListener(this);
    }

    public void onClick(Widget sender) {
      if (widget == sender) {
        int col = 0;
        while (getOwner().getColumnDef(col) != this)
          ++col;
        if (widget.isChecked()) {
          for (int row = 1; row < getOwner().getRowCount(); ++row) {
            CellRenderer r = (CellRenderer) getOwner().getWidget(row, col);
            if ("false".equals(r.getValue())) r.setValue("true");
          }
        } else {
          for (int row = 1; row < getOwner().getRowCount(); ++row) {
            CellRenderer r = (CellRenderer) getOwner().getWidget(row, col);
            if ("true".equals(r.getValue())) r.setValue("false");
          }
        }
        getOwner().fireCellWidgetClick(getOwner(), widget, 0, col);
      }
    }
  }

}
